<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <div>
            <span>姓名:</span>
            <input type="text" v-model="name" />
        </div>
        <div>
            <span>年龄:</span>
            <input type="number" v-model="age" />
        </div>
        <div>
            <span>性别:</span>
            <select v-model="gender">
                <option value="男">男</option>
                <option value="女">女</option>
            </select>
        </div>
        <div>
            <button @click="addOrEdit">添加/修改</button>
        </div>
        <div>
            <table border="1" cellpadding="10" cellspacing="0">
                <tr>
                    <th>序号</th>
                    <th>姓名</th>
                    <th>年龄</th>
                    <th>性别</th>
                    <th>操作</th>
                </tr>


                <tbody v-if="list.length >0">
                    <tr v-for="(item,index) in list " :key="item.id">
                        <td>{{index+1}}</td>
                        <td>{{item.name}}</td>
                        <td>{{item.age}}</td>
                        <td>{{item.gender}}</td>
                        <td>
                            <button @click="del(item.id)">删除</button>
                            <button @click="edit(item.id)">编辑</button>
                        </td>
                    </tr>
                </tbody>


                <tbody v-else>
                    <tr style="text-align: center;">
                        <td colspan="5">暂无数据</td>

                    </tr>
                </tbody>

            </table>
        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script>
        let flag = true
        const app = new Vue({
            el: '#app',
            data: {
                list: [
                    { id: '1001', name: '张三', age: 25, gender: '男' },
                    { id: '1002', name: '李四', age: 20, gender: '男' },
                    { id: '1003', name: '小丽', age: 18, gender: '女' },
                ],
                id: '',
                name: '',
                age: '',
                gender: '男'
            },
            methods: {
                del(id) {
                    if (confirm('你确定要删除吗?')) {
                        this.list = this.list.filter(k => k.id !== id)
                    }

                },

                //编辑
                edit(id) {
                    const arr = this.list.filter(k => k.id === id)
                    this.id = arr[0]['id']
                    this.name = arr[0]['name']
                    this.age = arr[0]['age']
                    this.gender = arr[0]['gender']
                    flag = !flag

                },
                addOrEdit() {

                    if (!this.name || !this.age || !this.gender) {
                        return alert('请检查信息,不允许为空!')
                    }
                    //编辑
                    if (!flag) {
                        const index = this.list.findIndex(k => k.id === this.id)
                        this.list[index].name = this.name
                        this.list[index].age = this.age
                        this.list[index].gender = this.gender
                        this.name = ''
                        this.age = ''
                        this.gender = ''
                        flag = !flag

                    } else {
                        //新增
                        console.log();
                        this.list.unshift({
                            id: +new Date(),
                            name: this.name,
                            age: this.age,
                            gender: this.gender
                        })
                        this.name = ''
                        this.age = ''
                        this.gender = ''
                    }
                }
            }
        })
    </script>
</body>

</html>